﻿using System;
using System.Collections.Generic;
using System.Configuration.Abstractions;
using System.Net.Mail;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Synchronization.Validation.Log;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;
using Sustainalytics.Synchronization.Validation.Validators;
using Xunit;

namespace Sustainalytics.Synchronization.Validation.Tests.IntegrationTest
{
    public class MailClientTest
    {
        private readonly MailClient _sut;
        private const string MongodbClientConnectionString = "mongodb://54.72.160.155:27017/ClientDW_B";

        public MailClientTest()
        {
            var appSettings = ConfigurationManager.Instance.AppSettings;
            var corporateDataLoader = new CorporateDataLoader(MongoFactory.GetCollectionFromDatabase<CorporateData>(MongodbClientConnectionString));
            var rules = new RulesList();

            _sut = new MailClient(
                new EmailSettings(appSettings),
                new SmtpClient(
                               appSettings.Get("SMTPServer"),
                               int.Parse(appSettings.Get("SMTPServerPort")))
                {
                    Credentials =
                                                         new System.Net.NetworkCredential(
                                                             appSettings.Get("SMTPUserName"),
                                                             appSettings.Get("SMTPPassword")),
                    EnableSsl = true
                },
                                    "Integration-Test",
                                    new MessageFormatter(corporateDataLoader),
                                    rules);
        }

        [Fact]
        public void SendMailTest()
        {
            var triggers = new List<string>() { "CG.4.4", "CG.1.10" };

            var fakes = new List<ValidationFailure>
                            {
                                new ValidationFailure(new Guid("aa4598f5-0434-4e4c-b5d9-71a6eb90d106"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Red, BusinessResearchProduct.Comprehensive,"RR1", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("16bf1242-06e6-4a78-9e86-36fc4bd0b9e8"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Red, BusinessResearchProduct.Comprehensive,"RR1", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("2e312100-8338-466e-980f-3a2ea4cfb156"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Red, BusinessResearchProduct.CorporateGovernance,"RR8", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("d8f38328-5730-4db2-a36c-002281de5dcd"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Red, BusinessResearchProduct.Comprehensive,"RR1", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("aa4598f5-0434-4e4c-b5d9-71a6eb90d106"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Yellow, BusinessResearchProduct.Comprehensive,"YR1", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("aa4598f5-0434-4e4c-b5d9-71a6eb90d106"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Yellow, BusinessResearchProduct.CorporateGovernance,"YR111", "InformationalMessage fro the rule",triggers),
                                new ValidationFailure(new Guid("aa4598f5-0434-4e4c-b5d9-71a6eb90d106"),"The company {0} #{1}# does not have compliant KEI issues comments", Severity.Yellow, BusinessResearchProduct.Comprehensive,"YR5", "InformationalMessage fro the rule",triggers),
                            };

            _sut.WriteAsync(fakes);
        }
    }
}
